package pkmn;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import pkmn.item.BallItem;
import pkmn.item.BattleItem;
import pkmn.item.Item;
import pkmn.item.KeyItem;
import pkmn.item.StatusItem;
import pkmn.item.TrainerItem;

public class Bag {

    private List<TrainerItem> battleItens;
    private List<TrainerItem> statusItens;
    private List<TrainerItem> ballItens;
    private List<KeyItem> keyItens;

    /**
     * Constructor.
     */
    public Bag() {
        this.battleItens = new ArrayList<TrainerItem>();
        this.statusItens = new ArrayList<TrainerItem>();
        this.ballItens = new ArrayList<TrainerItem>();
        this.keyItens = new ArrayList<KeyItem>();
    }

    /**
     * Insert a item on bag.
     * @param item Item
     */
    public void addItem(Item item) {
        this.addItem(item, 1);
    }

    /**
     * Insert itens on bag.
     * @param item Item
     * @param quantity int
     */
    public void addItem(Item item, int quantity) {
        TrainerItem trainerItem;
        int remained;
        if (quantity == 0)
            return;
        if (item instanceof BattleItem) {
            trainerItem = new TrainerItem((BattleItem) item);
            for (TrainerItem t : this.battleItens) {
                if (t.equals(trainerItem) && t.getQuantity() < 99) {
                    remained = t.addQuantity(quantity);
                    this.addItem(item, remained);
                    return;
                }
            }
            this.battleItens.add(trainerItem);
            Collections.sort(battleItens);

        } else if (item instanceof StatusItem) {
            trainerItem = new TrainerItem((StatusItem) item);
            for (TrainerItem t : this.statusItens) {
                if (t.equals(trainerItem)) {
                    remained = t.addQuantity(quantity);
                    this.addItem(item, remained);
                    return;
                }
            }
            this.statusItens.add(trainerItem);
            Collections.sort(statusItens);
        } else if (item instanceof BallItem) {
            trainerItem = new TrainerItem((BallItem) item);
            for (TrainerItem t : this.ballItens) {
                if (t.equals(trainerItem)) {
                    remained = t.addQuantity(quantity);
                    this.addItem(item, remained);
                    return;
                }
            }
            this.ballItens.add(trainerItem);
            Collections.sort(ballItens);
        } else if (item instanceof KeyItem) {
            for (KeyItem k : this.keyItens)
                if (k.equals((KeyItem) item))
                    return;
            keyItens.add((KeyItem) item);
            Collections.sort(keyItens);
        }
    }

    @Override
    public String toString() {
        String ret = "Battle Itens:";
        for (TrainerItem t : this.battleItens) {
            ret = ret + "\n-" + t;
        }
        ret = ret + "\nStatus Itens:";
        for (TrainerItem t : this.statusItens) {
            ret = ret + "\n-" + t;
        }
        ret = ret + "\nBalls:";
        for (TrainerItem t : this.ballItens) {
            ret = ret + "\n-" + t;
        }
        ret = ret + "\nKey Itens:";
        for (KeyItem k : this.keyItens) {
            ret = ret + "\n-" + k;
        }

        return ret;
    }
}
